﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Services.Protocols;



public partial class batch : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Load listbox from SQL Employee table.
    
    }

    protected void Generate_Resume(object sender, EventArgs e)
    {
        //Variables
        string userid;
        string OutputPath;
        string cvReportPath;
        string extension;
        string SaveFormat;
        DataTable dtResumes = new DataTable();

        //1. Get userID for folder creation
        //string userid = HttpContext.Current.User.Identity.Name.Replace("domain\\", "").ToLower();  //production
        userid = tbUsername.Text;

        //create network share to save resumes
        OutputPath = CVGenerator.CVMakeDir(userid);

        //2. Determine file extension and output format
        if (rblResumeFormat.SelectedValue == "word")
        {
            SaveFormat = "WORDOPENXML";
            extension = "docx";
        }
        else
        {
            SaveFormat = "PDF";
            extension = "pdf";
        }

        //3. Determine CV Template for ReportPath
        if (rblResumeTemplate.SelectedValue == "cvJJ")
        {
            cvReportPath = @"/EU Resource/CV_JJ";
        }
        else
        {
            cvReportPath = @"/EU Resource/CV_V5";
        }

        // 4. Set Datatable columns
        dtResumes.Columns.Add(new DataColumn("ReportPath"));
        dtResumes.Columns.Add(new DataColumn("ReportFormat"));
        dtResumes.Columns.Add(new DataColumn("Extension"));
        dtResumes.Columns.Add(new DataColumn("OutputPath"));
        dtResumes.Columns.Add(new DataColumn("SavedFile"));
        dtResumes.Columns.Add(new DataColumn("ReportParam"));

        //5. Create Datatable from selected employees and above variables. savedfile column will be
        //OutputPath + item name +"_" + item value+"."+ extension
        foreach (ListItem item in lbEmployeeList.Items)
        {
            if (item.Selected)
            {
                if (dtResumes != null)
                {
                    // Create a new DataRow in the DataTable
                    DataRow dr = dtResumes.NewRow();
                    dr["ReportPath"] = cvReportPath;
                    dr["ReportFormat"] = SaveFormat;
                    dr["Extension"] = extension;
                    dr["OutputPath"] = OutputPath + "\\";
                    if (rblResumeTemplate.SelectedValue == "cvJJ")
                    {
                        dr["SavedFile"] = item.Text.Replace("'", "") + "_" + item.Value + "_JJ" + "." + extension;
                    }
                    else
                    {
                        dr["SavedFile"] = item.Text.Replace("'", "") + "_" + item.Value + "." + extension;
                    }
                    dr["ReportParam"] = item.Value;
                    dtResumes.Rows.Add(dr);
                }
            }
            
        }
        // Bind created datatable to gridview to check if correct
        DataView dv = new DataView(dtResumes);
        gvSelectedEmployees.DataSource = dv;
        gvSelectedEmployees.DataBind();

        hlSavedPath.Text = OutputPath;

        //6. Pass datatable to CVGenerator class
        CVGenerator2.cvBatchResume(dtResumes);
        
    }
}